﻿using System;
using Solartron.Orbit3;

namespace NDK.Module.Model
{
    public class OrbitPPG
    {
        private static log4net.ILog m_Logger = log4net.LogManager.GetLogger("PPG");
        OrbitServer Orbit = new OrbitServer();
        const int NETINDEX = 0;
        bool bExitLoop = false;
        public bool OrbitConnection()
        {
            try
            {
                if (!Orbit.Connected)
                {
                    // connect to Orbit
                    Orbit.Connect();
                    if (Orbit.Connected == true)
                    {
                        FindHotswappable();
                        ZeroReadingInUnitsOffset = new double[Orbit.Networks.Count];
                        ZeroReadingInCountsOffset = new int[Orbit.Networks.Count];
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception Ex)
            {
                m_Logger.Error(Ex.StackTrace + "ERROR");
                return false;
            }
            return false;
        }

        public bool DisConnection()
        {
            try
            {
                if (Orbit.Connected == true)
                {
                    Orbit.Disconnect();
                }
            }
            catch (Exception Ex)
            {
                m_Logger.Error(Ex.StackTrace + "ERROR");
                return false;
            }
            return true;
        }

        private void FindHotswappable()
        {
            if (Orbit.Connected == true)
            {
                try
                {
                    // Find hotswappable modules
                    for (int i = 0; i < Orbit.Networks.Count; i++)
                    {
                        Orbit.Networks[i].Modules.FindHotswapped();
                    }
                }
                catch (Exception Ex)
                {
                    m_Logger.Error(Ex.StackTrace + "FindHotswappableERROR");
                }
            }
        }

        public double ReadInUnits(int PPGID)
        {
            double data = double.MinValue;
            if (Orbit.Connected == true)
            {
                try
                {
                    if (PPGID < Orbit.Networks.Count)
                    {
                        data = Orbit.Networks[PPGID].Modules[0].ReadingInUnits - ZeroReadingInUnitsOffset[PPGID];
                        return data;
                    }
                }
                catch (Exception Ex)
                {
                    m_Logger.Error(Ex.StackTrace + "ReadInUnitsERROR");
                    return data;
                }
            }
            return data;
        }

        #region Zero
        private int[] ZeroReadingInCountsOffset;

        private double[] ZeroReadingInUnitsOffset;

        public void InitialiseZeroing()
        {
            //Create the offset arrays - based upon the number of modules
            try
            {
               
                for (int i = 0; i < Orbit.Networks.Count; i++)
                {
                    ZeroReadingInCountsOffset[i] = Orbit.Networks[i].Modules[0].ReadingInCounts;
                    ZeroReadingInUnitsOffset[i] = Orbit.Networks[i].Modules[0].ReadingInUnits;
                }
            }
            catch (Exception Ex)
            {
                m_Logger.Error(Ex.StackTrace + "ZeroReadingsERROR");
            }
        }

        /// <summary>
        /// Reset the readings. This sets the reading offset to zero (which will remove any zero applied by 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ResetZero()
        {
            try
            {
                if (ZeroReadingInCountsOffset != null && ZeroReadingInUnitsOffset != null)
                {
                    for (int i = 0; i < Orbit.Networks.Count; i++)
                    {
                        ZeroReadingInCountsOffset[i] = 0;
                        ZeroReadingInUnitsOffset[i] = 0;
                    }
                }
            }
            catch (Exception Ex)
            {
                m_Logger.Error(Ex.StackTrace + "ResetZeroERROR");
            }
        }
        #endregion
    }
}
